import sys
from PyQt5 import QtCore, QtWidgets
import ui_dlgstd
from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtSql
import myglobal
from delegates import ComboBoxDelegateQt
import os
import myglobal
import traceback
from delegates import FloatEditDelegateQt
class doubleDelegate(QtWidgets.QItemDelegate):
    def createEditor(self,parent, option,index):
        editor =QtWidgets.QLineEdit(parent)
        return editor;
    def setEditorData(self,editor, index):
        value = index.model().data(index)
        editor.setText(str(value))
    def setModelData(self,editor,model,index):
        value = editor.text()
        model.setData(index, value)
    def updateEditorGeometry(self,editor,option,index):
        editor.setGeometry(option.rect)
class DlgStd(QtWidgets.QDialog):
    ui=ui_dlgstd.Ui_DialogClass();
    def __init__(self, parent=None):
        super().__init__( parent)
        self.ui.setupUi(self)
        self.setWindowTitle("标样")
        self.model = QtSql.QSqlRelationalTableModel(self);
        self.model.setTable("STD");
        self.model.select();
        self.ui.tableView.setModel(self.model);
        self.ui.tableView.setSortingEnabled(True);
        self.ui.tableView.setColumnWidth(1, 200);
        self.ui.tableView.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.edit_delegate=FloatEditDelegateQt()
        for i in range(self.model.columnCount()):
            self.ui.tableView.setItemDelegateForColumn(
                i, self.edit_delegate)
        self.ui.pushButton_import.clicked.connect(self.import_click)
        self.ui.pushButton_add.clicked.connect(self.add_click)
        self.ui.pushButton_delete.clicked.connect(self.delete_click)
        # self.ui.pushButton_query.clicked.connect(self.query_click)
        self.ui.lineEdit_query.textChanged.connect(self.query_click)
        self.ui.pushButton_query.setVisible(False)
        self.resize(800,600)
    def query_click(self):
        txt=self.ui.lineEdit_query.text()
        if txt!="":
            filt="sampleid like '%"+txt+"%'"
            print(filt)
            self.model.setFilter(filt)
        else:
            self.model.setFilter("")
        self.model.select()
    def import_click(self):
        FileDialog =QtWidgets.QFileDialog(self)
        folder=os.path.join(os.curdir)#os.path.expanduser('~'), "Desktop")
        fil = "xlsx Files (*.xlsx);;All Files (*)"
        FileDirectory = FileDialog.getOpenFileName(
            self, 'select import file', folder, fil)  # 选择目录，返回选中的路径
        fname = FileDirectory[0]
        if fname == "":
            pass
        else:
            try:
                myglobal.db.import_std_file2(fname)
                self.model.select()
            except:
                traceback.print_exc() 
    def add_click(self):
        # myglobal.db.
        self.model.insertRow(0)
        pass
    def delete_click(self):
        current=self.ui.tableView.currentIndex();
        row=current.row();
        self.model.removeRow(row)
        pass
    @pyqtSlot()
    def accept(self):
        self.model.submitAll();
        self.done(1)
    @pyqtSlot()    
    def reject(self):
        self.done(0)
if __name__ == "__main__":
    from PyQt5 import QtCore, QtWidgets
    import sys
    a = QtWidgets.QApplication(sys.argv)
    a.lastWindowClosed.connect(a.quit)
    w =Dlgstandard()
    w.show()
    sys.exit(a.exec_())